package com.cw.jeeyt.controller.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cw.jeeyt.api.dto.response.JsonResultDto;
import com.cw.jeeyt.common.DataTable;
import com.cw.jeeyt.common.ResultData;
import com.cw.jeeyt.common.enums.EnumOperateFlag;
import com.cw.jeeyt.common.web.BaseController;
import com.cw.jeeyt.service.bo.system.SystemProvinceBo;
import com.cw.jeeyt.service.directive.PageDictDirective;
import com.cw.jeeyt.service.directive.PermissionDirective;
import com.cw.jeeyt.service.intf.system.SystemProvinceService;
import com.cw.lang.common.utils.CollectionUtil;
import com.cw.lang.mybatis.dto.PageInfo;

/**
 * 省份信息控制类
 * @author chenw
* @version 1.0.0
 */
@Controller
@RequestMapping("/systemProvince")
public class SystemProvinceController extends BaseController {
    @Resource
    private SystemProvinceService systemProvinceService;//省份信息业务实现类

    /**
     * 跳转到列表查询页面
     *
     * @param systemProvinceBo
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/query")
    public ModelAndView query(SystemProvinceBo systemProvinceBo) throws Exception {
        ModelAndView mv = new ModelAndView("system/systemProvince_index");
        mv.addObject("countryId", systemProvinceBo.getCountryId());
        mv.addObject("permission", new PermissionDirective());
        return mv;
    }

    /**
     * 加载DataTable列表数据
     *
     * @param dataTable 请求数据
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/listSystemProvince")
    public ResultData listSystemProvince(@RequestBody DataTable dataTable) throws Exception {

        SystemProvinceBo systemProvinceBo = JSONObject.parseObject(JSON.toJSONString(dataTable.getQueryCond()),
            SystemProvinceBo.class);
        if (systemProvinceBo == null) {
            systemProvinceBo = new SystemProvinceBo();
        }
        PageInfo<SystemProvinceBo> pageInfo = systemProvinceService.queryByPage(systemProvinceBo, dataTable.getStart(),
            dataTable.getLength());
        return ResultData.builder()
            .draw(dataTable.getDraw())
            .data(CollectionUtil.isNotEmpty(pageInfo.getList())?pageInfo.getList():new ArrayList())
            .recordsTotal(pageInfo.getTotal())
            .recordsFiltered(pageInfo.getTotal())
            .build();
    }

    /**
     * 跳转到表单页面
     *
     * @param id 主键
     * @param operation 操作  add 新增 edit修改 view查看
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/to-form")
    public ModelAndView toForm(String id, String operation) throws Exception {
        ModelAndView mv = new ModelAndView("system/systemProvince_form");
        SystemProvinceBo systemProvinceBo = null;
        if (EnumOperateFlag.ADD.getCode()
            .equals(operation)) {
            systemProvinceBo = new SystemProvinceBo();
        } else {
            systemProvinceBo = systemProvinceService.getSystemProvinceById(id);
        }
        mv.addObject("systemProvince", systemProvinceBo);
        mv.addObject("operation", operation);
        mv.addObject("PageDict", new PageDictDirective());
        return mv;
    }

    /**
     * 根据操作类型保存数据
     *
     * @param systemProvinceBo
     * @param operation add新增  edit修改 view查看
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/saveSystemProvince", method = RequestMethod.POST)
    public JsonResultDto saveSystemProvince(SystemProvinceBo systemProvinceBo, String operation) throws Exception {

        JsonResultDto jsonResultDto = new JsonResultDto(false);
        try {
            if (operation.equals(EnumOperateFlag.ADD.getCode())) {
                systemProvinceService.insertSystemProvince(systemProvinceBo);
            } else if (operation.equals(EnumOperateFlag.EDIT.getCode())) {
                systemProvinceService.updateSystemProvince(systemProvinceBo);
            }
            jsonResultDto.setMsg("保存成功");
            jsonResultDto.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResultDto.setMsg("保存失败:" + e.getMessage());
        }
        jsonResultDto.setOperation(operation);
        return jsonResultDto;
    }

    /**
     * 删除省份信息
     *
     * @param systemProvinceBo
     * @throws Exception
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public JsonResultDto deleteSystemProvince(SystemProvinceBo systemProvinceBo) {

        JsonResultDto jsonResultDto = new JsonResultDto(false);
        try {
            systemProvinceService.deleteSystemProvince(systemProvinceBo);
            jsonResultDto.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();

            jsonResultDto.setMsg("删除失败：" + e.getMessage());
        }
        return jsonResultDto;
    }

    /**
     * 跳转到省市联动页面
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/to-selectAddress")
    public ModelAndView toSelectAddress(HttpServletRequest request) throws Exception {
        ModelAndView mv = new ModelAndView("system/systemProvince_selectAddress");
        return mv;
    }

    /**
     * 加载省份信息
     *
     * @param countryCode
     * @throws Exception
     */
    @RequestMapping(value = "/load_province")
    @ResponseBody
    public JsonResultDto loanProvince(String countryCode) {

        JsonResultDto jsonResultDto = new JsonResultDto(false);
        try {
            Map<String, Object> map = new HashMap<>();
            List<SystemProvinceBo> systemProvinceList = systemProvinceService.loadProvinceByCountryCode(countryCode);
            map.put("systemProvinceList", systemProvinceList);
            jsonResultDto.setResultData(map);
            jsonResultDto.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResultDto.setMsg("加载失败：" + e.getMessage());
        }
        return jsonResultDto;
    }
}
